// pages/login/index/index.js
import { WeChatZhaohuAppletLogin } from '../../../apis/modules/wechat';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isReadPass : false,
    openId:"",
    unionid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.autoLogin===undefined){
        this.autoLogin(false);
      }

  },

  // 自动登录
  autoLogin: function(isNextPage) {
    Toast.loading({
        message: '加载中...',
        forbidClick: true,
        loadingType: 'spinner',
      });
    wx.login({
        success (res) {
          if (res.code) {
            WeChatZhaohuAppletLogin({js_code:res.code}).then((res) => {
                if(res.data.userToken == null && res.data.userSessionInfo.openId!=''){
                    const openId=res.data.userSessionInfo.openId;
                    const unionId=res.data.userSessionInfo.unionId;
                    if(isNextPage){
                        wx.navigateTo({url: `/pages/login/login-send-code/login-send-code?openId=${openId}&unionid=${unionId}`})
                    }
                }else{
                    app.globalData.userInfo = res.data.userInfo;
                    app.globalData.userToken =res.data.userToken;
                    app.globalData.userSessionInfo=res.data.userSessionInfo;
                    app.globalData.userIsLogin = true;
                    app.globalData.userEmployeeId=res.data.userInfo.employeeId;
                    app.globalData.userCompanyId=res.data.userInfo.companyId;
                    var loginInfo= JSON.stringify(res.data);
                    wx.setStorage({key:"login-info",data:loginInfo})
                    wx.switchTab({
                        url: '/pages/care/care-index/care-index',
                    })
                }
            })
          } 
        }
      })
  },
  isReadPassOnChangeEvent:function(event){
    this.setData({
        isReadPass: event.detail,
      });
  },
  clickUserAgreementEvent:function(){
    wx.navigateTo({url: '/pages/login/user-agreement/user-agreement'})
  },
  clickPrivacyPolicyEvent:function(){
    wx.navigateTo({url: '/pages/login/privacy-policy/privacy-policy'})
  },
  clickWechatAuthLoginEvent:function(){
    if(this.data.isReadPass===false){
        Toast('请点击我已阅读并同意')
        return;
    }
    if(this.data.openId!==''){
        wx.navigateTo({url: `/pages/login/login-send-code/login-send-code?openId=${this.data.openId}&unionid=${this.data.unionid}`})
    }else{
        this.autoLogin(true);
    }
  },
  setOpenIdAndUnionIdEvent:function(unionid,openId){
    this.setData({unionid:unionid,openId:openId})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})